define([
	"UtilDir/util",
	"UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
	"Date","DateCN", "css!DateCss",
	"EasyUI","EasyUI-lang"
    ],function(Util, Tool, SelectOrg, SearchBlock){

    var sysPath =  getServer() + "/static/app/zdyh/classifygroup";

    var zfzxClassifyGroupFormHtml = sysPath + "/views/zfzxClassifyGroupForm.html";
	var getZfzxClassifyGroupController = function(){
		return getServer() + "/qh/zfzxClassifyGroups";
	};

	/**
	 * 页面初始化
	 */
	var init = function(){
        initSearchBlock();
        queryBtnBind();
		createZfzxClassifyGroupGrid();
	};


	var initSearchBlock = function(){
        SearchBlock.init("searchBlock");
	};
	var queryBtnBind = function(){
        $("#btnSearch").click(function () {
            createZfzxClassifyGroupGrid();
        });
        $("#btnReset").click(function () {
			$("#idCondition").val("");
				$("#groupIdCondition").val("");
				$("#groupNameCondition").val("");
				$("#isValidCondition").val("");
				$("#createTimeCondition").val("");
				$("#createUserCondition").val("");
				$("#updateTimeCondition").val("");
				$("#updateUserCondition").val("");
				$("#remarkCondition").val("");
			});
	};
	var createZfzxClassifyGroupGrid= function() {
        $("#zfzxClassifyGroupGrid").datagrid({
            url:getZfzxClassifyGroupController() + "/queryZfzxClassifyGroups",
			method:"GET",
            fitColumns: true,
            autoRowHeight: false,
            columns:[[
				{
					field:'id',
					title:'主键ID',
					width:'',
					align:'center',
				},
				{
					field:'groupId',
					title:'分组ID',
					width:'',
					align:'center',
				},
				{
					field:'groupName',
					title:'分组名称',
					width:'',
					align:'center',
				},
				{
					field:'isValid',
					title:'是否有效,0:无效,1:有效',
					width:'',
					align:'center',
				},
				{
					field:'createUser',
					title:'创建人',
					width:'',
					align:'center',
				},
				{
					field:'createTime',
					title:'创建时间',
					width:'',
					align:'center',
				},
				{
					field:'updateTime',
					title:'修改时间',
					width:'',
					align:'center',
				},
				{
					field:'updateUser',
					title:'修改人',
					width:'',
					align:'center',
				},
				{
					field:'remark',
					title:'备注',
					width:'',
					align:'center',
				},
            ]],
            toolbar: [{
                iconCls: 'fa fa-plus-circle',
                text:"添加",
                handler: function(){
                    addZfzxClassifyGroup();
                }
            },{
                iconCls: 'fa fa-trash-o',
                text:"删除",
                handler: function(){
                    deleteZfzxClassifyGroup();
                }
            }],
            queryParams:{
				id: $("#idCondition").val(),
				groupId: $("#groupIdCondition").val(),
				groupName: $("#groupNameCondition").val(),
				isValid: $("#isValidCondition").val(),
				createTime: $("#createTimeCondition").val(),
				createUser: $("#createUserCondition").val(),
				updateTime: $("#updateTimeCondition").val(),
				updateUser: $("#updateUserCondition").val(),
				remark: $("#remarkCondition").val(),
            },
            pagination: true,
            pageSize: 20
        });
    };
	var formValidator = function(){
		$("#zfzxClassifyGroupForm").validate({
			rules : {
				id : {
					required : true,
				},
				groupId : {
					required : true,
				},
				groupName : {
					required : true,
				},
			},
			messages : {
				id : {
					required : "主键ID不允许为空!",
				},
				groupId : {
					required : "分组ID不允许为空!",
				},
				groupName : {
					required : "分组名称不允许为空!",
				},
			}
		});
	};
	var getZfzxClassifyGroup = function(id){
		$.ajax({
			url: getZfzxClassifyGroupController() + "/"+id,
			type: "get",
			success: function (data) {
				Tool.deserialize("zfzxClassifyGroupForm", data);
			}
		});
	};

	var addZfzxClassifyGroup = function () {
		var slidebar = Util.slidebar({
			url: zfzxClassifyGroupFormHtml,
			width: "580px",
			cache: false,
			close : true,
			afterLoad: function () {
				formValidator();
				$("#saveBtn").on("click", function () {
					if($("#zfzxClassifyGroupForm").valid()){
						var data = Tool.serialize("zfzxClassifyGroupForm");
						$.ajax({
							url: getZfzxClassifyGroupController() ,
                            contentType:"application/json",
							data: JSON.stringify(data),
							type: "post",
							success: function (data) {
								Util.alert(data.message);
								slidebar.close();
                                createZfzxClassifyGroupGrid();
							}
						});
					}
				});
			}
		});
	};
	window._editZfzxClassifyGroup = function(zfzxClassifyGroupId) {
		var slidebar = Util.slidebar({
			url: zfzxClassifyGroupFormHtml,
			width: "580px",
			cache: false,
			close : true,
			afterLoad: function () {
				formValidator();
				getZfzxClassifyGroup(zfzxClassifyGroupId);
				$("#saveBtn").on("click", function () {
					if($("#zfzxClassifyGroupForm").valid()){
						var data = Tool.serialize("zfzxClassifyGroupForm");
						$.ajax({
							url: getZfzxClassifyGroupController(),
                            contentType:"application/json",
                            data: JSON.stringify(data),
							type: "put",
							success: function (data) {
								Util.alert(data.message);
								slidebar.close();
								createZfzxClassifyGroupGrid();
							}
						});
					}
				});
			}
		});
	};
	var deleteZfzxClassifyGroup = function() {
		var rows = $("#zfzxClassifyGroupGrid").datagrid("getSelections");
		if (rows == null || rows.length == 0) {
			Util.alert("请选择一行数据!");
			return;
		}
		Util.confirm("是否要删除选中的数据?", function() {
			var ids = "";
			$.each(rows, function(i, row){
				ids += row.id + ",";

			});
			ids = ids.substr(0,ids.length - 1);
			$.ajax({
				url: getZfzxClassifyGroupController() ,
				data: {
					ids : ids
				},
				type: "delete",
				success: function (data) {
					createZfzxClassifyGroupGrid();
				}
			});
		}, function() {
			return;
		});

	};

	return {
		init:init
	};
});
